const initialState = {
  loading: true,
  error: false,
  articleList: [],
};

const ActionTypes = {

     LOAD_ARTICLES : 'LOAD_ARTICLES',
     LOAD_ARTICLES_SUCCESS : 'LOAD_ARTICLES_SUCCESS',
     LOAD_ARTICLES_ERROR : 'LOAD_ARTICLES_ERROR',
}

export function loadArticles() {
  return {
    types: [ActionTypes.LOAD_ARTICLES, ActionTypes.LOAD_ARTICLES_SUCCESS, ActionTypes.LOAD_ARTICLES_ERROR],
    url: '/api/articles.json',
  };
}

export default function previewList(state = initialState, action) {
  switch (action.type) {
    case ActionTypes.LOAD_ARTICLES: {
      return {
        ...state,
        loading: true,
        error: false,
      };
    }

    case ActionTypes.LOAD_ARTICLES_SUCCESS: {
      return {
        ...state,
        loading: false,
        error: false,
        articleList: action.payload,
      };
    }

    case ActionTypes.LOAD_ARTICLES_ERROR: {
      return {
        ...state,
        loading: false,
        error: true,
      };
    }

    default:
      return state;
  }
}